import * as vscode from 'vscode';
import { SnippetManager } from '../utils/snippetManager';
import { SnippetProvider } from '../providers/snippetProvider';

export class CreateSnippetCommand {
    constructor(
        private snippetManager: SnippetManager,
        private snippetProvider: SnippetProvider
    ) {}

    get commandId(): string {
        return 'snippetManager.createSnippet';
    }

    async execute(): Promise<void> {
        try {
            // 获取当前选中的文本作为默认代码
            const editor = vscode.window.activeTextEditor;
            const selectedText = editor?.document.getText(editor.selection) || '';

            // 输入代码片段名称
            const name = await vscode.window.showInputBox({
                prompt: 'Enter snippet name',
                placeHolder: 'e.g., React Component',
                validateInput: value => value ? null : 'Name is required'
            });
            if (!name) return;

            // 输入描述
            const description = await vscode.window.showInputBox({
                prompt: 'Enter snippet description',
                placeHolder: 'Brief description of the snippet'
            });
            if (!description) return;

            // 选择语言
            const languages = [
                'javascript', 'typescript', 'python', 'java', 'cpp', 'csharp', 'php', 'go', 'rust', 'html', 'css', 'json', 'markdown'
            ];
            const language = await vscode.window.showQuickPick(languages, {
                placeHolder: 'Select language for this snippet'
            });
            if (!language) return;

            // 输入分组
            const existingGroups = this.snippetManager.getGroups();
            const group = await vscode.window.showInputBox({
                prompt: 'Enter group name',
                placeHolder: existingGroups.length > 0 ? `Existing: ${existingGroups.join(', ')}` : 'e.g., Frontend, Backend',
                validateInput: value => value ? null : 'Group is required'
            });
            if (!group) return;

            // 输入标签
            const tagsInput = await vscode.window.showInputBox({
                prompt: 'Enter tags (comma-separated)',
                placeHolder: 'e.g., react, component, ui'
            });
            const tags = tagsInput ? tagsInput.split(',').map(t => t.trim()).filter(t => t) : [];

            // 输入代码
            const code = await vscode.window.showInputBox({
                prompt: 'Enter the code snippet',
                value: selectedText || '',
                valueSelection: [0, 0]
            });
            if (!code) return;

            // 创建代码片段
            await this.snippetManager.createSnippet({
                name,
                description,
                language,
                code,
                tags,
                group
            });

            this.snippetProvider.refresh();
            vscode.window.showInformationMessage(`Snippet "${name}" created successfully!`);

        } catch (error) {
            vscode.window.showErrorMessage(`Failed to create snippet: ${error}`);
        }
    }
}
